/*
 *  Copyright 2010 Mahbub Mozadded Sowrov.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package com.sowrov.util.logsystem;

import java.io.IOException;

import java.net.URL;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import java.util.Date;


/**
 * LogFormatter implementation for plain text.
 *
 * @author Sowrov
 * @version %I% %G%
 */
public class PlainText implements LogFormatter {
    private SimpleDateFormat dateFormat;

    public PlainText() {
        this.dateFormat = new SimpleDateFormat("dd MMM yyyy hh:mm:ss a");
    } // end PlainText()

    public String getCriticalFormat(
                                    final String message, final int lineNo, final String functionName,
                                    final String fileName) {
        StringBuilder format = new StringBuilder("------- ");
        format.append(LevelManager.getLevelName(LogLevel.CRITICAL)).append(" Error -------\n");
        format.append("Location:");
        format.append(fileName).append("@");
        format.append(lineNo);
        format.append("->");
        format.append(functionName).append("(...)\n");
        format.append("Time: ");
        format.append(this.dateFormat.format(new Date())).append("\n\n");

        format.append("Cause: ");
        format.append(message).append("\n");
        format.append("------------------------------\n\n");

        return format.toString();
    } // end getCriticalFormat()

    public String getDebugFormat(
                                 final String message, final int lineNo, final String functionName,
                                 final String fileName) {
        Date date = new Date();
        StringBuilder format = new StringBuilder(LevelManager.getLevelName(LogLevel.DEBUG)+" "+date + ": ");
        format.append(fileName).append("@");
        format.append(lineNo).append("->");
        format.append(functionName).append("(...)\n>> ");
        format.append(message).append("\n");

        return format.toString();
    } // end getDebugFormat()

    public String getFatalFormat(
                                 final String message, final int lineNo, final String functionName,
                                 final String fileName) {
        StringBuilder format = new StringBuilder("------- ");
        format.append(LevelManager.getLevelName(LogLevel.FATAL)).append(" Error -------\n");
        format.append("Location:");
        format.append(fileName).append("@");
        format.append(lineNo);
        format.append("->");
        format.append(functionName).append("(...)\n");
        format.append("Time: ");
        format.append(this.dateFormat.format(new Date())).append("\n\n");

        format.append("Cause: ");
        format.append(message).append("\n");
        format.append("---------------------------\n\n");

        return format.toString();
    } // end getFatalFormat()

    public String getFinalFormat() {
        return "-----------Logging Completed At " + this.dateFormat.format(new Date()) + "-----------\n";
    } // end getFinalFormat()

    public String getInfoFormat(
                                final String message, final int lineNo, final String functionName,
                                final String fileName) {
        StringBuilder format = new StringBuilder(LevelManager.getLevelName(LogLevel.INFO) + ": ");
        format.append(fileName).append("@");
        format.append(lineNo).append("->");
        format.append(functionName).append("(...)\n>> ");
        format.append(message).append("\n");

        return format.toString();
    } // end getInfoFormat()

    public String getIntroFormat() {
        URL fileURL = this.getClass().getResource("PlainText.class");
        Date compileDate = null;

        try {
            compileDate = new Date(fileURL.openConnection().getLastModified());
        } // end try
        catch (final IOException ex) {
        } // end catch

        String dateTime = "Unknown";

        if (compileDate != null) {
            dateTime = this.dateFormat.format(compileDate);
        } // end if

        String format = "Last Compile Time: ";
        format += dateTime;
        format += "\nLogging Started at: ";
        format += (this.dateFormat.format(new Date()) + "\n\n");

        return format;
    } // end getIntroFormat()

    public String getWarningFormat(
                                   final String message, final int lineNo, final String functionName,
                                   final String fileName) {
        StringBuilder format = new StringBuilder("<!>");
        format.append(LevelManager.getLevelName(LogLevel.WARNING)).append("<!>").append(new Date()).append("\n");
        format.append(fileName).append("@");
        format.append(lineNo).append("->");
        format.append(functionName).append("(...)\n>> ");
        format.append(message).append("\n\n");

        return format.toString();
    } // end getWarningFormat()
} // end PlainText
